﻿Public Class SMSMessages
    Inherits SystemCollection(Of SMSMessage)

    Private WithEvents _oSMS As SMSMessage
    Private blnHasUnread As Boolean = False

    Public Event ReadMessagesChanged(UnreadMessages As Boolean)

    Public Property HasUnReadMessages As Boolean
        Get
            Return blnHasUnread
        End Get
        Set(value As Boolean)
            blnHasUnread = value
        End Set
    End Property

    Protected Overrides Function AddNewCore() As Object

        Dim _oSMS As New SMSMessage
        _oSMS.Name = "New Message"

        Add(_oSMS)

        MyBase.OnAddingNew(New System.ComponentModel.AddingNewEventArgs(_oSMS))

        Return _oSMS

    End Function

    Public Overloads Sub Add(SMS As SMSMessage)

        MyBase.Add(SMS)

        CheckReadMessages()

    End Sub


    Private Sub _oSMS_PropertyChanged(sender As Object, e As System.ComponentModel.PropertyChangedEventArgs) Handles _oSMS.PropertyChanged

        If e.PropertyName = "Read" Then
            CheckReadMessages()
        End If

    End Sub

    Private Sub CheckReadMessages()

        Dim s As SMSMessage
        For Each s In MyBase.Items
            If s.Read = False Then
                blnHasUnread = True
                Exit For
            End If
            blnHasUnread = False
        Next

        RaiseEvent ReadMessagesChanged(blnHasUnread)

    End Sub

    Private Sub SMSMessages_ListChanged(sender As Object, e As System.ComponentModel.ListChangedEventArgs) Handles Me.ListChanged

        CheckReadMessages()

    End Sub
End Class
